<?php

namespace fztadmin\modules\user\models\forms;

use common\components\fenzhangtong\FenZhangTong;
use common\queues\CustomSetSettlementCard;
use common\Tools;
use common\queues\LogFengZhangTongSend;
use wodrow\yii2wtools\tools\ArrayHelper;
use wodrow\yii2wtools\tools\Model;

class CustomBankCard extends \common\models\db\CustomBankCard
{
    const SCENARIO_CHECKSMS = 'check-sms';

    public $oriTransId;
    public $phoneDynCode;
    public $transAmt;

    public function scenarios()
    {
        $scenarios = parent::scenarios();
        $scenarios = ArrayHelper::merge($scenarios, [
            self::SCENARIO_CHECKSMS => ['oriTransId', 'phoneDynCode'],
        ]);
        return $scenarios;
    }

    public function rules()
    {
        $rules = parent::rules();
        foreach ($rules as $k => $v) {
            if ($v[1] == 'required'){
                $rules[$k][0] = array_diff($rules[$k][0], ['created_at', 'updated_at', 'created_by', 'updated_by']);
            }
        }
        $rules = ArrayHelper::merge($rules, [
            [['oriTransId', 'phoneDynCode'], 'required', 'on' => self::SCENARIO_CHECKSMS],
        ]);
        return $rules;
    }

    public function attributeLabels()
    {
        $attributeLabels = parent::attributeLabels();
        $attributeLabels = ArrayHelper::merge($attributeLabels, [
            'oriTransId' => "申请业务流水号",
            'phoneDynCode' => "手机验证码",
        ]);
        return $attributeLabels;
    }

    public function bindNew(FenZhangTong &$fzt)
    {
        $transId = uniqid();
        $params = [
            'transId' => $transId,
            'platCustNo' => $this->plat_cust_no,
            'mobileNo' => $this->mobile_no,
            'openBank' => $this->open_bank,
            'cardNo' => $this->card_no,
            'payCode' => $this->pay_code,
        ];
        $fzt->IESI111101($params);
        $log = new LogFengZhangTongSend();
        $log->fzt = $fzt;
        $log->forModelClass = CustomBankCard::class;
        switch ($fzt->ret_code){
            case "00000":
                \Yii::$app->session->addFlash("success", "申请成功，请验证");
                \Yii::$app->queue->push($log);
                return true;
                break;
            case "E010110112":
                if (!$this->save()) {
                    \Yii::$app->session->addFlash("error", Model::getModelError($this));
                    \Yii::$app->queue->push($log);
                    return false;
                }
                $log->forModelId = $this->id;
                \Yii::$app->queue->push($log);
                \Yii::$app->session->addFlash("error", $fzt->ret_msg);
                return false;
                break;
            default:
                \Yii::$app->session->addFlash("error", "请求分账通异常：".Tools::toJson($fzt->resp_arr));
                \Yii::$app->queue->push($log);
                return false;
                break;
        }
    }

    public function bindNewCheck(FenZhangTong &$fzt)
    {
        $transId = uniqid();
        $params = [
            'transId' => $transId,
            'platCustNo' => $this->plat_cust_no,
            'oriTransId' => $this->oriTransId, // 新增绑卡申请时的对应请求业务流水号
            'phoneDynCode' => $this->phoneDynCode, // 会员收到的短信验证码
        ];
        $params['transAmt'] = $this->transAmt ?? null; // 转账金额（企业绑卡必传）
        $fzt->IESI111102($params);
        $log = new LogFengZhangTongSend();
        $log->fzt = $fzt;
        $log->forModelClass = CustomBankCard::class;
        switch ($fzt->ret_code){
            case "00000":
                if (!$this->save()) {
                    \Yii::$app->session->addFlash("error", Model::getModelError($this));
                    \Yii::$app->queue->push($log);
                    return false;
                }
                \Yii::$app->session->addFlash("success", "验证成功");
                $log->forModelId = $this->id;
                \Yii::$app->queue->push($log);
                return true;
                break;
            default:
                \Yii::$app->session->addFlash("error", "请求分账通异常：".Tools::toJson($fzt->resp_arr));
                \Yii::$app->queue->push($log);
                return false;
                break;
        }
    }

    public function setSettlementCard(FenZhangTong &$fzt)
    {
        $transId = uniqid();
        $params = [
            'transId' => $transId,
            'platCustNo' => $this->plat_cust_no,
            'cardNo' => $this->card_no,
            'notifyUrl' => \Yii::$app->apiTool->generateUrl("/notify/fzt-custom-set-settlement-card", [
                'card_id' => $this->id,
            ]), // 通知回调地址
            'returnUrl' => \Yii::$app->apiTool->generateUrl("/notify/fzt-custom-set-settlement-card", [
                'card_id' => $this->id,
                'redirect_url' => urlencode(\Yii::$app->urlManager->createAbsoluteUrl("/site/index")),
            ]), // 交易完成后页面跳转地址
        ];
        $fzt->IESI111103($params);
        $log = new LogFengZhangTongSend();
        $log->fzt = $fzt;
        $log->forModelClass = CustomBankCard::class;
        $log->forModelId = $this->id;
        \Yii::$app->queue->push($log);
        switch ($fzt->ret_code){
            case "00000":
                return true;
                break;
            case "E010110105":
                \Yii::$app->session->addFlash("error", $fzt->ret_msg);
                \Yii::$app->queue->push(new CustomSetSettlementCard(['customBankCardId' => $this->id]));
                $fzt->resp_arr['retUrl'] = \Yii::$app->urlManager->createAbsoluteUrl("/site/index");
                return true;
                break;
            default:
                \Yii::$app->session->addFlash("error", "请求分账通异常：".Tools::toJson($fzt->resp_arr));
                return false;
                break;
        }
    }
}